#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wiringPi.h>
#include <MQTTClient.h>

#define MQTT_BROKER_ADDRESS "tcp://mqtt.broker.com" // 替换为您的 MQTT 代理地址
#define MQTT_TOPIC "mytopic" // 替换为您的 MQTT 主题
#define GPIO_PIN 0 // 替换为您要使用的 GPIO 引脚

volatile int message_received = 0;

void messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
    if (strncmp(message->payload, "gas", message->payloadlen) == 0) {
        printf("Received MQTT message: %.*s\n", message->payloadlen, (char *)message->payload);
        digitalWrite(GPIO_PIN, HIGH); // 设置 GPIO0 引脚为高电平
    } else {
        printf("Received unknown MQTT message: %.*s\n", message->payloadlen, (char *)message->payload);
        digitalWrite(GPIO_PIN, LOW); // 设置 GPIO0 引脚为低电平
    }
    message_received = 1;
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
}

int main(int argc, char *argv[]) {
    wiringPiSetup();
    pinMode(GPIO_PIN, OUTPUT);

    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;

    MQTTClient_create(&client, MQTT_BROKER_ADDRESS, "ExampleClient", MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    if (MQTTClient_connect(client, &conn_opts) != MQTT